package com.example.textswitchtestdemo01.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.bean.AllProductionLine;
import com.example.textswitchtestdemo01.util.InitToolbar;
import com.example.textswitchtestdemo01.util.RetrofitUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FifthActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_title;
    private Button btn_mpv_line;
    private Button btn_jiaoche_line;
    private Button btn_suv_line;
    private Button btn_position1;
    private Button btn_position2;
    private Button btn_position3;
    private Button btn_position4;
    private Button btn_create;
    private List<AllProductionLine.DataBean> list = new ArrayList<>();
    private int lineId = 0;
    private int positon = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fifth);
        initView();
        InitToolbar.initToolbar(this, "第五页", true);
        getAllInfo();


    }

    private void setSomeEnabled(int chooseLine) {
        btn_position1.setEnabled(true);
        btn_position2.setEnabled(true);
        btn_position3.setEnabled(true);
        btn_position4.setEnabled(true);
        lineId = chooseLine;

        for (AllProductionLine.DataBean bean : list) {
            if (chooseLine == bean.getProductionLineId()) {
                switch (bean.getPosition()) {
                    case 1:
                        btn_position1.setEnabled(false);
                        break;
                    case 2:
                        btn_position2.setEnabled(false);
                        break;
                    case 3:
                        btn_position3.setEnabled(false);
                        break;
                    case 4:
                        btn_position4.setEnabled(false);
                        break;
                }
            }
        }
    }

    private void getAllInfo() {
        RetrofitUtil.getAllProductionLine(new Callback<AllProductionLine>() {
            @Override
            public void onResponse(Call<AllProductionLine> call, Response<AllProductionLine> response) {
                AllProductionLine body = response.body();
                Toast.makeText(FifthActivity.this, body.getMessage(), Toast.LENGTH_SHORT).show();
                List<AllProductionLine.DataBean> data = body.getData();
                if (data != null && !data.isEmpty()) {
                    list.clear();
                    list.addAll(data);
                }

            }

            @Override
            public void onFailure(Call<AllProductionLine> call, Throwable t) {

            }
        });
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        btn_mpv_line = (Button) findViewById(R.id.btn_mpv_line);
        btn_jiaoche_line = (Button) findViewById(R.id.btn_jiaoche_line);
        btn_suv_line = (Button) findViewById(R.id.btn_suv_line);
        btn_position1 = (Button) findViewById(R.id.btn_position1);
        btn_position2 = (Button) findViewById(R.id.btn_position2);
        btn_position3 = (Button) findViewById(R.id.btn_position3);
        btn_position4 = (Button) findViewById(R.id.btn_position4);
        btn_create = (Button) findViewById(R.id.btn_create);
        btn_mpv_line.setOnClickListener(this);
        btn_jiaoche_line.setOnClickListener(this);
        btn_suv_line.setOnClickListener(this);
        btn_position1.setOnClickListener(this);
        btn_position2.setOnClickListener(this);
        btn_position3.setOnClickListener(this);
        btn_position4.setOnClickListener(this);
        btn_create.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_mpv_line:
                setSomeEnabled(1);
                break;
            case R.id.btn_jiaoche_line:
                setSomeEnabled(2);
                break;
            case R.id.btn_suv_line:
                setSomeEnabled(3);
                break;
            case R.id.btn_position1:
                positon = 1;
                break;
            case R.id.btn_position2:
                positon = 2;
                break;
            case R.id.btn_position3:
                positon = 3;
                break;
            case R.id.btn_position4:
                positon = 4;
                break;
            case R.id.btn_create:
                RetrofitUtil.addProductionLine(new AllProductionLine.DataBean(1, 1, lineId, 0, positon, 0), new Callback<AllProductionLine>() {
                    @Override
                    public void onResponse(Call<AllProductionLine> call, Response<AllProductionLine> response) {
                        Toast.makeText(FifthActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<AllProductionLine> call, Throwable t) {
                        Toast.makeText(FifthActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
                getAllInfo();
                break;
        }
    }
}
